package Sort;

public class QuickSort {

	public int[] sortArray(int[] data, int left, int right) {
		if (left < right && left >= 0 && right >= 0 ){
			int indexPivot = right;
			int indexWall = left;
			for (int i = left; i< indexPivot ; i++){
				if (data[i] < data[indexPivot]){
					trySwap(data, i, indexWall);
					indexWall++;
				}
			}
			if (data[indexPivot] < data[indexWall]){
				trySwap(data, indexPivot, indexWall);
			}
			sortArray(data, left, indexWall-1);
			sortArray(data, indexWall+1, right);
		}
		return data;
	}

	private void trySwap(int[] data, int indexA, int indexB){
		int a = data[indexA];
		data[indexA] = data[indexB];
		data[indexB] = a;
	}

}
